
var apiFun = require('../../utils/api.js');

var data = {
  "magicId": 4,
  "title": "好像听撩人的话",
  "createTime": "2018-07-05 17:05:17",
  "colorScheme": {
    "themeId": 2,
    "themeName": "黄色系",
    "isDelete": 1,
    "bgColor": "-webkit-linear-gradient(top, rgb(234,185,45) 0%,rgb(199,152,16) 100%)",
    "navBgColor": "#E5B900",
    "navFontColor": "#ffffff",
    "qBgColor": "#ffffff",
    "qAuthorColor": "#ffffff",
    "qContentFontColor": "black",
    "aBgColor": "#ffffff",
    "aAuthorColor": "#ffffff",
    "aContentFontColor": "black",
    "buttonFontColor": "black",
    "buttonBgColor": "rgba(255, 255, 255, 0.5)"
  }
};

var footerConfig = {
  'isFooterMagic': true,
  'isFooterZb': false,
  'fontColor': '#3D3D3D',
  'lineColor': '#3D3D3D',
  'fontSelectedColor': '#3D3D3D',
}

var currentPage = 1;
var nextPage    = 2;

Page({

  onPullDownRefresh: function (event) {
    var that = this;
    var isMagicDescribe = this.data.isMagicDescribe;
    if (this.data.isMagicDescribe == false) {

      this.setData({
        isMagicLoading: false
      })

    } else {
      this.setData({
        isLoadingStart: false
      })
    }

    //wx.startPullDownRefresh({})
    
    var statusRefresh = this.data.statusRefresh;
    if (statusRefresh == 0) {
        console.log('正式下拉刷新数据');
        initLoadData(that, 'cover');

    } else {
      statusRefresh = statusRefresh-1;
      this.setData({
        statusRefresh:statusRefresh
      })
    }

    //wx.startPullDownRefresh({})
    //initLoadData(that, 'cover');
    //wx.stopPullDownRefresh();

  },

  onReachBottom:function(){
    wx.startPullDownRefresh({
      success: function (res) {
        //console.log('上拉操作');
      }
    })
  },

  //init data
  data: {
    list: {},
    listIndex: false,
    statusRefresh:0
  },

  onShow: function () {
    console.log('qie huan 22');

    var listIndex = this.data.listIndex;

    //设置底部颜色
    if (listIndex === false){

      //默认原始颜色
      wx.setTabBarStyle({
        color: '#000000',
        selectedColor: '#000000',
        backgroundColor: '#ffffff',
        borderStyle: 'white',
        fail: function (res) {
          //console.log(res);
        }
      })

    } else {
      wx.setTabBarStyle({
        color: '#ffffff',
        selectedColor: '#EDEDED',
        backgroundColor: this.data.list[listIndex]['colorScheme']['navBgColor'],
        borderStyle: 'white',
        fail: function (res) {
          //console.log(res);
        }
      })

    }
  },

  //onLoad
  onLoad: function (event) {
    var that = this;

    //set data
    this.setData({
      isLoadingStart: true
    })

    initLoadData(that, 'cover');
  },

  onBegin: function (event) {

    var that = this;

    //set data
    this.setData({
      isLoadingStart: true
    })

    initLoadData(that, 'cover');
  },

  /**
   *
   * 问魔镜
   * request answer magic
   */
  eventAnswerMagic: function (event) {

    var that = this;

    var dataset = event.currentTarget.dataset;
    var magicId = dataset.magicId;
    var indexId = dataset.indexId;
    var currentThemeId = dataset.currentThemeId;
    var currentContentId = dataset.currentContentId;

    var changeMagicList = this.data.list;

    that.setData({
      loadingMagic: true,
      isMagicLoading: true
    })


    apiFun.questionMagic(
      {
        magicId: magicId,
        userId: 2,
        currentThemeId: currentThemeId,
        currentContentId: currentContentId,
        success: function (res) {

          changeMagicList[indexId]['magicContent'] = res.magicContent;
          changeMagicList[indexId]['magicContentId'] = res.magicContentId;
          changeMagicList[indexId]['magicTitle'] = changeMagicList[indexId]['title'];
          changeMagicList[indexId]['isShowOneytitle'] = true;
          //changeMagicList[indexId]['colorScheme'] = res.colorScheme;

          that.setData({
            list: changeMagicList,
            loadingMagic: false,
            isMagicLoading: false
          })

          /*设置bar颜色
          var firstBgColor = res.colorScheme.navBgColor;
          var firstFrontColor = res.colorScheme.navFontColor;
          wx.setNavigationBarColor({
            frontColor: firstFrontColor,
            backgroundColor: firstBgColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          })
       
          wx.setTabBarStyle({
            color: '#ffffff',
            selectedColor: '#EDEDED',
            backgroundColor: firstBgColor,
            borderStyle: 'white',
            fail: function (res) {
              console.log(res);
            }
          })*/


        },
        fail: function (res) {
          return false;
        }

      },

    )

  },

  /**
   * 上下滑动问题
   * @param  {[type]} event [description]
   * @return {[type]}       [description]
   */
  eventChangeMagicContent: function (event) {

    

    var that = this;

    // 当前位置
    var current = event.detail.current;

    //var magicInfo = this.data.magicList[current];

    //如果滑动到最后，需要加载新的数据
    if ((current + 1) % 10 == 0) {
      if (this.data.magicPage.pageSize != '') {
        if (this.data.magicPage.pageNumber < this.data.magicPage.pageTotalNumber) {

          var nextPageNumber = this.data.magicPage.pageNumber + 1;

          //追加列表
          api.getMagicList(
            {
              success: function (res) {
                that.setData({
                  list: that.data.magicList.concat(res.list),
                  page: res.page,
                })
              },
              pageNumber: nextPageNumber,
              userId: 2
            },
          )
        }
      }
      //console.log('load more data');
    }


    if (current == 0) {
      this.setData({
        statusRefresh:1
      })
    } else {
      var statusRefresh = 2;
      this.setData({
        statusRefresh:statusRefresh
      })
    }

    /*设置bar上的颜色*/
    wx.setNavigationBarColor({
      frontColor: this.data.list[current]['colorScheme']['navFontColor'],
      backgroundColor: this.data.list[current]['colorScheme']['navBgColor'],
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })

    wx.setTabBarStyle({
      color: '#ffffff',
      selectedColor: '#EDEDED',
      backgroundColor: this.data.list[current]['colorScheme']['navBgColor'],
      borderStyle: 'white',
      fail:function(res){
        //console.log(res);
      }
    })

    //设置当前元素位置
    this.setData({
      listIndex: current
    })
  },

  jumpMagic: function (event) {
    wx.reLaunch({
      url: '/pages/mc_new_u/mc/'
    })
  },
  JumpZb: function (event) {
    wx.reLaunch({
      url: '/pages/zb_new/zb'
    })
  }

})



function setTarBarColor(frontColor, backgroundColor)
{
  wx.setNavigationBarColor({
    frontColor: frontColor,
    backgroundColor: backgroundColor,
    animation: {
      duration: 400,
      timingFunc: 'easeIn'
    }
  })
}

/**
 * 初始化数据
 *
 * var dataChangetype string [concat/cover]
 */
function initLoadData(that, dataChangetype='cover'){


  //console.log(nextPage);

  //request
  apiFun.getMagicList(
    {
      success: function (res) {

        if (res.list != false) {

          //设置第一个data的bar颜色
          var firstBgColor    = res.list[0]['colorScheme']['navBgColor'];
          var firstFrontColor = res.list[0]['colorScheme']['navFontColor'];
          wx.setNavigationBarColor({
            frontColor: firstFrontColor,
            backgroundColor: firstBgColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          })

          //设置底部颜色
          wx.setTabBarStyle({
            color: '#ffffff',
            selectedColor: '#EDEDED',
            backgroundColor: firstBgColor,
            borderStyle: 'white',
            fail: function (res) {
              console.log(res);
            }
          })

          that.setData({
            list: res.list,
            page: res.page,
            isMagicDescribe: false,
            isLoadingStart: false,
            listIndex:0
          })

        } else {
          //errorMessage = '出问题啦， 没有数据啦';
          //isReloadAction = true;
        }
      },
      fail: function (res) {
        that.setData({
          //isLoadAnimation: false,
          //isReloadAction: true,
          //errorMessage: '网络通信出错拉。。。'
        })
      },
      pageNumber: 1,
      userId: 2
    },
  )
}